/**
 * Coryright 2015-2020 www.eyaomall.cn
 * All rights reserved.
 * @author tumingjun
 * @date 2016年8月12日
 *
 */

package com.eyaomall.wxmall.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eyaomall.wxmall.common.helper.MemberHelper;
import com.eyaomall.wxmall.common.util.StringFilterUtil;
import com.eyaomall.wxmall.model.po.Goods;
import com.eyaomall.wxmall.model.po.Shop;
import com.eyaomall.wxmall.model.vo.GoodsVO;
import com.eyaomall.wxmall.service.CategoryService;
import com.eyaomall.wxmall.service.CollectService;
import com.eyaomall.wxmall.service.GoodsImgService;
import com.eyaomall.wxmall.service.GoodsService;
import com.eyaomall.wxmall.service.ShopService;
import com.lycheeframework.core.cmp.api.http.GsnData;
import com.lycheeframework.core.cmp.api.http.GsnResponse;
import com.lycheeframework.core.cmp.kit.PageHTTPObject;

/**
 * 商品
 * 
 * @author yuhuafeng
 * @date 2016-9-7
 */
@Controller
@RequestMapping("goods")
public class GoodsController {

	@Autowired
	private GoodsService goodsService;

	@Autowired
	private GoodsImgService goodsImgService;

	@Autowired
	private CategoryService categoryService;

	@Autowired
	private CollectService collectService;

	@Autowired
	private ShopService shopService;

	/**
	 * 热销商品
	 * 
	 * @return
	 */
	@RequestMapping("hot_goods")
	public @ResponseBody GsnData hotGoods() {

		return GsnResponse.toData(goodsService.hotGoods());
	}

	/**
	 * 分页显示商品列表
	 */
	@RequestMapping("page_data")
	public @ResponseBody GsnData pageData(GoodsVO goodsVO, PageHTTPObject httpObject) {
		goodsVO.setShopId(MemberHelper.getShopId());
		goodsVO.setIsPublish("y");
		return GsnResponse.toData(goodsService.page(goodsVO, httpObject));
	}

	/**
	 * 进入商品列表页
	 */
	@RequestMapping("page")
	public String goodsPage(String serach, Map<String, Object> map) {
		serach = StringFilterUtil.SimpleStringFilter(serach);
		map.put("serach", serach);
		map.put("category", categoryService.queryByShopId(MemberHelper.getShopId()));
		return "goods/index";
	}

	/**
	 * 商品详情
	 */
	@RequestMapping("goodsDetail")
	public String goodsDetail(Integer id, Map<String, Object> map) {
		Goods goods = goodsService.query(id);// 根据传入的Id查询商品
		Shop shop = shopService.queryById(goods.getShopId());// 查询店铺信息
		map.put("collect", collectService.queryByGoodsIDAndMemberId(id, MemberHelper.getId()));
		map.put("img", goodsImgService.queryImgByGoodsId(id));
		map.put("goods", goods);
		map.put("shop", shop);
		return "goods/goodsDeatil";
	}

	/**
	 * 热销商品列表
	 * 
	 * @return
	 */
	@RequestMapping("hotgoods_list")
	public @ResponseBody GsnData hotGoods(Goods goods, PageHTTPObject httpObject) {
		return GsnResponse.toData(goodsService.hotGoods(goods, httpObject));
	}
}
